package comgame.entities;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.Spawns;
import comgame.components.CropComponent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.util.Objects;

public class CropFactory implements EntityFactory {
    public enum CropType {
        WHEAT, POTATO, TOMATO,CARROT,STARFRUIT
    }


    // 提取公共创建逻辑到独立方法
    private Entity createCropEntity(SpawnData data, String texturePath, CropType type) {
        Image image = new Image(Objects.requireNonNull(
                getClass().getResourceAsStream(texturePath)));

        ImageView imageView = new ImageView(image);

        // 配置视图属性
        double targetSize = 80;
        imageView.setSmooth(true);
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(targetSize);
        imageView.setStyle("-fx-border-color: red; -fx-border-width: 1px;");

        return FXGL.entityBuilder(data)
                .type(type)
                .viewWithBBox(imageView)
                .zIndex(100)
                .build();
    }

    // 小麦
    @Spawns("WHEAT")
    public Entity createWheat(SpawnData data) {
        Entity wheat = createCropEntity(data,
                "/assets/textures/Seed_Wheat.png",
                CropType.WHEAT);

        // 为小麦添加特定组件
        wheat.addComponent(new CropComponent("WHEAT", "未成熟"));
        wheat.addComponent(new CropComponent(50));
        return wheat;
    }

    //土豆
    @Spawns("POTATO")
    public Entity createPotato(SpawnData data) {
        Entity potato = createCropEntity(data,
                "/assets/textures/Seed_Potato.png",
                CropType.POTATO);


        // 为土豆添加特定组件
        potato.addComponent(new CropComponent("POTATO", "未成熟"));
        potato.addComponent(new CropComponent(50));
        return potato;
    }

    //西红柿
    @Spawns("TOMATO")
    public Entity createTomato(SpawnData data) {
        Entity tomato = createCropEntity(data,
                "/assets/textures/Seed_Tomato.png",
                CropType.TOMATO);

        // 为西红柿添加特定组件
        tomato.addComponent(new CropComponent("tomato", "未成熟"));
        tomato.addComponent(new CropComponent(50));
        return tomato;
    }

    //胡萝卜
    @Spawns("CARROT")
    public Entity createCarrot(SpawnData data) {
        Entity carrot = createCropEntity(data,
                "/assets/textures/Seed_Caroto.png",
                CropType.CARROT);

        // 为胡萝卜添加特定组件
        carrot.addComponent(new CropComponent("CARROT", "未成熟"));
        carrot.addComponent(new CropComponent(50));
        return carrot;
    }

            //星果
    @Spawns("STARFRUIT")
    public Entity createStarfruit(SpawnData data) {
        Entity starfruit = createCropEntity(data,
                "/assets/textures/Seed_Starfruit.png",
                CropType.STARFRUIT);

        // 为星果添加特定组件
        starfruit.addComponent(new CropComponent("STARFRUIT", "未成熟"));
        starfruit.addComponent(new CropComponent(50));
        return starfruit;
    }


}
